package com.ruoyi.experiment.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.experiment.service.IExperimentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.experiment.domain.EmergencyPlan;
import com.ruoyi.experiment.service.IEmergencyPlanService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 应急预案Controller
 * 
 * @author gw
 * @date 2025-01-08
 */
@RestController
@RequestMapping("/plan")
public class EmergencyPlanController extends BaseController
{
    @Autowired
    private IEmergencyPlanService emergencyPlanService;

    @Autowired
    private IExperimentService experimentService;

    /**
     * 查询应急预案列表
     */
    @RequiresPermissions("Experiment:plan:list")
    @GetMapping("/list")
    public TableDataInfo list(EmergencyPlan emergencyPlan)
    {
        startPage();
        List<EmergencyPlan> list = emergencyPlanService.selectEmergencyPlanList(emergencyPlan);
        for (EmergencyPlan emergencyPlan1 : list){
            emergencyPlan1.setExperimentName(experimentService.selectExperimentByExperimentId(emergencyPlan1.getExperimentId()).getExperimentName());
        }
        return getDataTable(list);
    }

    /**
     * 导出应急预案列表
     */
    @RequiresPermissions("Experiment:plan:export")
    @Log(title = "应急预案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmergencyPlan emergencyPlan)
    {
        List<EmergencyPlan> list = emergencyPlanService.selectEmergencyPlanList(emergencyPlan);
        ExcelUtil<EmergencyPlan> util = new ExcelUtil<EmergencyPlan>(EmergencyPlan.class);
        util.exportExcel(response, list, "应急预案数据");
    }

    /**
     * 获取应急预案详细信息
     */
    @RequiresPermissions("Experiment:plan:query")
    @GetMapping(value = "/{planId}")
    public AjaxResult getInfo(@PathVariable("planId") Long planId)
    {
        return success(emergencyPlanService.selectEmergencyPlanByPlanId(planId));
    }

    /**
     * 新增应急预案
     */
    @RequiresPermissions("Experiment:plan:add")
    @Log(title = "应急预案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmergencyPlan emergencyPlan)
    {
        return toAjax(emergencyPlanService.insertEmergencyPlan(emergencyPlan));
    }

    /**
     * 修改应急预案
     */
    @RequiresPermissions("Experiment:plan:edit")
    @Log(title = "应急预案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmergencyPlan emergencyPlan)
    {
        return toAjax(emergencyPlanService.updateEmergencyPlan(emergencyPlan));
    }

    /**
     * 删除应急预案
     */
    @RequiresPermissions("Experiment:plan:remove")
    @Log(title = "应急预案", businessType = BusinessType.DELETE)
	@DeleteMapping("/{planIds}")
    public AjaxResult remove(@PathVariable Long[] planIds)
    {
        return toAjax(emergencyPlanService.deleteEmergencyPlanByPlanIds(planIds));
    }
}
